package com.space.controller.no;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.space.base.ApiResponse;
import com.space.base.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api1")
@Slf4j
@Api("配置接口")
public class WalkerController {

    private String filePre="visualUi-templates";

    {
//        File t = new File(System.getProperty("user.dir") + File.separator + filePre);
//        if(!t.exists()) t.mkdirs();
    }

    @PostMapping("/add")
    @ApiOperation("buildFile")
    public ApiResponse<String> buildFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            String filePath = System.getProperty("user.dir") + File.separator + filePre +File.separator+ obj.getString("id")+".json";
            File file = new File(filePath);
            if(file.exists()) file.delete();
            // 使用try-with-resources语句自动关闭资源
            try (PrintWriter out = new PrintWriter(new FileWriter(filePath))) {
//                for(int i=mapInfo.maps.length-1;i>=0;i--){
//                    for(int j=0;j<mapInfo.maps[0].length;j++){
////                    out.print(mapInfo.maps[i][j]+1);
//                        if(h>mapInfo.maps[i][j])  out.print(0);
//                        else out.print(1);
//                    }
//                    out.println();
//                }
                out.print(obj.getString("data"));
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }

    @PostMapping("/update")
    @ApiOperation("update")
    public ApiResponse<String> update(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            String filePath = System.getProperty("user.dir") + File.separator + filePre +File.separator+ obj.getString("id")+".json";
            File file = new File(filePath);
            if(file.exists()) file.delete();
            try (PrintWriter out = new PrintWriter(new FileWriter(filePath))) {
                out.print(obj.getString("data"));
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }


    }


    @PostMapping("/removeByIds")
    @ApiOperation("deleteFile")
    public ApiResponse<String> deleteFile(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            JSONArray ids= obj.getJSONArray("ids");
            Iterator<Object> i = ids.iterator();
            while (i.hasNext()){
                Object j=  i.next();
                String filePath = System.getProperty("user.dir") + File.separator + filePre +File.separator+ j+".json";
                File file = new File(filePath);
                if(file.exists()) file.delete();
            }



            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }



    @GetMapping("/removeAll")
    @ApiOperation("deleteAllFile")
    public ApiResponse<String> deleteAllFile(){
        try {

            String filePath = System.getProperty("user.dir") + File.separator  + filePre +File.separator;
            File file = new File(filePath);
            List<String> all =Arrays.stream(file.listFiles()).filter(s->s.isFile()&&s.getName().contains("json")).map(s->s.getName()).collect(Collectors.toList());

            for (String v:all){
                File file1 = new File(System.getProperty("user.dir") + File.separator + filePre +File.separator + v);
                if(file1.exists()) file1.delete();
            }



            ApiResponse<String> result = new ApiResponse();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @PostMapping("/findById")
    @ApiOperation("findFileById")
    public ApiResponse<String> findFileById(@RequestBody String v){
        try {
            JSONObject obj= JSON.parseObject(v, JSONObject.class);

            String filePath = System.getProperty("user.dir") + File.separator+ filePre +File.separator  + obj.getString("id")+".json";
            StringBuffer sb = new StringBuffer();
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            ApiResponse<String> result = new ApiResponse(sb.toString());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }

    @GetMapping("/findAll")
    @ApiOperation("findAll")
    public ApiResponse<String> findAll(){
        List<String> all = new ArrayList<>();
        try {

            File file = new File(System.getProperty("user.dir") + File.separator + filePre +File.separator) ;
            List<File> tmp = Arrays.stream(file.listFiles()).filter(s->s.getName().contains(".json")).collect(Collectors.toList());
            for(File v:tmp){
                String filePath =  v.getAbsolutePath();
                StringBuffer sb = new StringBuffer();
                try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                all.add(sb.toString());
            }

            ApiResponse<String> result = new ApiResponse(all);
            return result;
           } catch (Exception e) {
            e.printStackTrace();
            ApiResponse<String> result = new ApiResponse();
            result.setReCode(ErrorCode.ERROR);
            result.setReMsg(ErrorCode.ERROR_Msg);

            return result;
        }
    }


}
